#import "BaiduTracePlugin.h"
#import <BaiduTraceSDK/BaiduTraceSDK.h>
@interface BaiduTracePlugin ()<BTKEntityDelegate,BTKTraceDelegate>

@end

@implementation BaiduTracePlugin{
    FlutterMethodChannel *_channel;
}
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
  FlutterMethodChannel* channel = [FlutterMethodChannel
      methodChannelWithName:@"baidu_trace"
            binaryMessenger:[registrar messenger]];
  BaiduTracePlugin* instance = [[BaiduTracePlugin alloc] initWithChannel:channel];
  [registrar addMethodCallDelegate:instance channel:channel];
}

- (instancetype)initWithChannel:(FlutterMethodChannel *)channel {
    self = [super init];
    if (self) {
        _channel = channel;
    }
    return self;
}

- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
  if ([@"getPlatformVersion" isEqualToString:call.method]) {
    result([@"iOS " stringByAppendingString:[[UIDevice currentDevice] systemVersion]]);
  }else if([@"initSdk" isEqualToString:call.method]){
    [BTKPrivacyAuthorization setAgreePrivacy:YES];
     NSDictionary *dic = (NSDictionary *)call.arguments;
     BOOL r=[[BTKAuthManager sharedInstance] start:dic[@"ak"] generalDelegate:self];
     BTKServiceOption *serverOption = [[BTKServiceOption alloc] initWithAK:dic[@"ak"] mcode:dic[@"mcode"] serviceID:[dic[@"serviceId"] intValue] keepAlive:NO];
     if (!serverOption) result(@NO);
     BOOL flag = [[BTKAction sharedInstance] initInfo:serverOption];
     result(@(flag));
  }else if([@"addEntity" isEqualToString:call.method]){
     NSDictionary *dic = (NSDictionary *)call.arguments;
     NSInteger serviceId=[dic[@"serviceId"] intValue];
     NSInteger tag=[dic[@"tag"] intValue];
     BTKAddEntityRequest *request = [[BTKAddEntityRequest alloc] initWithEntityName:dic[@"entityName"] entityDesc:dic[@"entityDesc"] columnKey:dic[@"custom"] serviceID:serviceId tag:tag];
     [[BTKEntityAction sharedInstance] addEntityWith:request delegate:self];
     result(@YES);
  }else if([@"updateEntity" isEqualToString:call.method]){
      NSDictionary *dic = (NSDictionary *)call.arguments;
      NSInteger serviceId=[dic[@"serviceId"] intValue];
      NSInteger tag=[dic[@"tag"] intValue];
      BTKUpdateEntityRequest *request = [[BTKUpdateEntityRequest alloc] initWithEntityName:dic[@"entityName"] entityDesc:dic[@"entityDesc"] columnKey:dic[@"custom"] serviceID:serviceId tag:tag];
      [[BTKEntityAction sharedInstance] updateEntityWith:request delegate:self];
      result(@YES);
  }else if([@"startTrace" isEqualToString:call.method]){
      NSDictionary *dic = (NSDictionary *)call.arguments;
      BTKStartServiceOption *op = [[BTKStartServiceOption alloc] initWithEntityName:dic[@"entityName"]];
      // 开启服务
      [[BTKAction sharedInstance] startService:op delegate:self];
      result(@YES);
  }else if([@"stopTrace" isEqualToString:call.method]){
      [[BTKAction sharedInstance] stopService:self];
      result(@YES);
  }else if([@"startGather" isEqualToString:call.method]){
      [[BTKAction sharedInstance] showsBackgroundLocationIndicator:YES];
      [[BTKAction sharedInstance] startGather:self];
      result(@YES);
  }else if([@"stopGather" isEqualToString:call.method]){
      [[BTKAction sharedInstance] stopGather:self];
      result(@YES);
  } else {
    result(FlutterMethodNotImplemented);
  }
}

#pragma mark - BTKEntityDelegate

/**
 创建Entity终端实体的回调方法

 @param response 创建结果
 */
- (void)onAddEntity:(NSData *)response {
    if (!_channel) return;
    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingAllowFragments error:nil];
    NSLog(@"add entity response: %@", dict);
    [_channel invokeMethod:@"addEntityCallBack" arguments:dict];
}

/**
 更新Entity终端实体的回调方法

 @param response 更新结果
 */
- (void)onUpdateEntity:(NSData *)response {
    if (!_channel) return;
    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingAllowFragments error:nil];
    NSLog(@"update entity response: %@", dict);
    [_channel invokeMethod:@"updateEntityCallBack" arguments:dict];
}

#pragma mark - BTKTraceDelegate

/**
 开启轨迹服务的回调方法

 @param error 开启轨迹服务的结果
 */
- (void)onStartService:(BTKServiceErrorCode)error {
    if (!_channel) return;
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[@"status"] = @(error);
    [_channel invokeMethod:@"onStartTraceCallback" arguments:dict];
}
/**
 停止轨迹服务的回调方法

 @param error 停止轨迹服务的结果
 */
- (void)onStopService:(BTKServiceErrorCode)error {
    if (!_channel) return;
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[@"status"] = @(error);
    [_channel invokeMethod:@"onStopTraceCallback" arguments:dict];
}

/**
 开始采集的回调方法

 @param error 开始采集的操作结果
 */
- (void)onStartGather:(BTKGatherErrorCode)error {
    if (!_channel) return;
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[@"status"] = @(error);
    [_channel invokeMethod:@"onStartGatherCallback" arguments:dict];
}
/**
 停止采集的回调方法

 @param error 停止采集的操作结果
 */
- (void)onStopGather:(BTKGatherErrorCode)error {
    if (!_channel) return;
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[@"status"] = @(error);
    [_channel invokeMethod:@"onStopGatherCallback" arguments:dict];
}

@end
